//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.PlugIns.RequestComparers
{
    /// <summary>An enumeration which represents the results of IEnumerable.Compare in a more explicit way.</summary>
    public enum CompareResult {
        /// <summary>Of the two requests, the first one passed in (the one on the left) is higher priority.</summary>
        LeftIsHigherPriority=-1, 
        /// <summary>The two requests are equal in their priority.</summary>
        PrioritiesAreEqual=0,
        /// <summary>Of the two requests, the second one passed in (the one on the right) is higher priority.</summary>
        RightIsHigherPriority=1}

    /// <summary>A base class for implementing IComparer but wihtout having to use the annoying return value of IComparer.Compare.</summary>
    /// <remarks>IComparer uses a simple <see cref="int"/> to represents the result of the compare.  
    /// This can be confusing, and so this class forwards the call to <see cref="Compare"/> by calling <see cref="CompareUris"/> which returns a <see cref="CompareResult"/>.</remarks>
    public abstract class BaseComparer : IComparer<Request>
    {
        #region IComparer<Uri> Members

        /// <summary>Compares two Uris and returns the which one to process first.</summary>
        /// <param name="leftRequest">The left request.</param>
        /// <param name="rightRequest">The right request.</param>
        /// <returns>The same values as a regular Compare function.  Items occcuring first in sort order are considered higher priority.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "1#"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#")]
        public int Compare(Request leftRequest, Request rightRequest)
        {
            switch (CompareUris(leftRequest, rightRequest))
            {
                case CompareResult.LeftIsHigherPriority:
                    return -1;
                case CompareResult.PrioritiesAreEqual:
                    return 0;
                default: //CompareResult.RightIsHigherPrioirity
                    return 1;
            }

        }

        #endregion
        /// <summary>Compares two <see cref="Uri"/>s to see which is higher priority.</summary>
        /// <param name="leftRequest">The left <see cref="Request"/> to compare.</param>
        /// <param name="rightRequest">The right <see cref="Request"/> to compare.</param>
        /// <returns>A <see cref="CompareResult"/> declaring if the left or right <see cref="Request"/> is higher priority.</returns>
        public abstract CompareResult CompareUris(Request leftRequest, Request rightRequest);
    }
}
